﻿using CommonTools;
using MotionIoLib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using System.Xml.Serialization;

namespace StationDemo
{
    public class SocketData
    {
        public int StationIndex { get; set; }
        public Dictionary<string, int> SocketNum { get; set; }//各工站在第一个工位时，当前所有治具对应的治具号
    }
    

    public  class SocketDataMgr
    {
        public static object _lock = new object();
        public static SocketDataMgr socketDataMgr = null;
        public static SocketDataMgr GetInstance()
        {
            if (socketDataMgr == null)
            {
                lock (_lock)
                { 
                    socketDataMgr = new SocketDataMgr();
                }
            }
            return socketDataMgr;
        }

        public Dictionary<int, bool> SocketEnanbleDic = new Dictionary<int, bool>();

        public Dictionary<string, int> dicTableSocketAndPos = new Dictionary<string, int>();

        public bool[] IsHaveProduct =  new bool[4] { false,false,false ,false };
        public bool b_是否退料 = false;

        public bool Function_治具是否有产品()
        {
            bool result = false;
            foreach (var item in IsHaveProduct)
            {
                result |= item;
            }
            return result;
        }

        #region  获取治具信息
        /// <summary>
        /// 刷新治具状态
        /// </summary>
        public void GetSocketDic()
        {
            if (!SocketEnanbleDic.ContainsKey(0))
            {
                SocketEnanbleDic.Add(0, Param_System.Instance.启用A工位._strParamVal);
                SocketEnanbleDic.Add(1, Param_System.Instance.启用B工位._strParamVal);
                SocketEnanbleDic.Add(2, Param_System.Instance.启用C工位._strParamVal);
                SocketEnanbleDic.Add(3, Param_System.Instance.启用D工位._strParamVal);
            }
            else
            {
                SocketEnanbleDic[0] = Param_System.Instance.启用A工位._strParamVal;
                SocketEnanbleDic[1] = Param_System.Instance.启用B工位._strParamVal;
                SocketEnanbleDic[2] = Param_System.Instance.启用C工位._strParamVal;
                SocketEnanbleDic[3] = Param_System.Instance.启用D工位._strParamVal;
            }

        }
        /// <summary>
        /// 获取当前工站对应的治具工位
        /// </summary>
        /// <param name="StationIndex"></param>
        /// <param name="dFine"></param>
        /// <returns></returns>

        public int GetSocketNum(int StationIndex = 1)
        {
            if (IOMgr.GetInstace().Function_ReCheckIO(IO_IN.工位判断接近1.ToString(), false, 50) &&  ///无螺丝感应，视为A工位
                IOMgr.GetInstace().Function_ReCheckIO(IO_IN.工位判断接近2.ToString(), false, 50))
            {
                return (StationIndex + 1) % 4;


            }
            else if(IOMgr.GetInstace().Function_ReCheckIO(IO_IN.工位判断接近1.ToString(), true, 50) &&  ///1有感应，2无螺丝感应，视为B工位
                IOMgr.GetInstace().Function_ReCheckIO(IO_IN.工位判断接近2.ToString(), true, 50))
            {
                return (StationIndex + 2) % 4;
            }
            else if (IOMgr.GetInstace().Function_ReCheckIO(IO_IN.工位判断接近1.ToString(), true, 50) &&  ///1\2有感应，视为C工位
               IOMgr.GetInstace().Function_ReCheckIO(IO_IN.工位判断接近2.ToString(), false, 50))
            {
                return (StationIndex + 3) % 4; ;
            }
            else if (IOMgr.GetInstace().Function_ReCheckIO(IO_IN.工位判断接近1.ToString(), false, 50) &&  ///1无感应,2有感应，视为D工位
             IOMgr.GetInstace().Function_ReCheckIO(IO_IN.工位判断接近2.ToString(), true, 50))
            {
                return StationIndex % 4; ;
            }
            return -1;
        }

        #endregion
    }
}
